package com.relax.netty.thirdexample;

import io.netty.channel.Channel;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
import io.netty.channel.group.ChannelGroup;
import io.netty.channel.group.DefaultChannelGroup;
import io.netty.handler.timeout.IdleStateEvent;
import io.netty.util.concurrent.GlobalEventExecutor;

public class MyChatServerHandler extends SimpleChannelInboundHandler<String> {

    private static ChannelGroup channelGroup = new DefaultChannelGroup(GlobalEventExecutor.INSTANCE);

    /**
     * 任何一个clinet发消息都是这个方法
     *
     * @param ctx
     * @param msg
     * @throws Exception
     */
    @Override
    protected void channelRead0(ChannelHandlerContext ctx, String msg) throws Exception {

        Channel channel = ctx.channel();
        System.out.println(channel.remoteAddress() + "发送的消息是:" + msg);
        channelGroup.forEach(ch -> {
            if (channel != ch) {
                ch.writeAndFlush(channel.remoteAddress() + "发送的消息是:" + msg + "\n");/**不加\n不能发消息*/
            } else {
                ch.writeAndFlush("自己发送的消息是:" + msg + "\n");
            }
        });
    }

    /**
     * 每一个客户端连接成功,后调用
     *
     * @param ctx
     * @throws Exception
     */
    @Override
    public void handlerAdded(ChannelHandlerContext ctx) throws Exception {
        Channel channel = ctx.channel();
        /**
         * 在添加到 channelGroup 前,先广播
         */
        channelGroup.writeAndFlush("服务器-" + channel.remoteAddress() + "- 加入\n");

        channelGroup.add(channel);
    }

    @Override
    public void handlerRemoved(ChannelHandlerContext ctx) throws Exception {
        Channel channel = ctx.channel();
        channelGroup.writeAndFlush("服务器-" + channel.remoteAddress() + "- 离开\n");
        /**
         * 客户端断开,channelGroup会自动去除这个channel,自己不需要自己写     channelGroup.remove(channel)
         */
    }

    @Override
    public void channelActive(ChannelHandlerContext ctx) throws Exception {
        Channel channel = ctx.channel();
        System.out.println(channel.remoteAddress() + "  已上线");
    }

    @Override
    public void channelInactive(ChannelHandlerContext ctx) throws Exception {
        Channel channel = ctx.channel();
        System.out.println(channel.remoteAddress() + "  下线");
    }

    @Override
    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
        super.exceptionCaught(ctx, cause);
        ctx.close();
    }

    @Override
    public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exception {
        if (evt instanceof IdleStateEvent) {
            IdleStateEvent event = (IdleStateEvent) evt;
            String eventTyoe = null;

            switch (event.state()) {
                case READER_IDLE:
                    eventTyoe = "读空闲";
                    break;
                case WRITER_IDLE:
                    eventTyoe = "写空闲";
                    break;
                case ALL_IDLE:
                    eventTyoe = "读写空闲";
                    break;
            }
            System.out.println(ctx.channel().remoteAddress() + "超时事件为:" + eventTyoe);
//            final Channel channel = ctx.channel();
//            channelGroup.remove(channel);
//            channel.close();
        }
    }
}
